<template>
  <el-cascader :value="value" :options="option" size="mini" clearable placeholder="请选择异常类型"
    :props="{ expandTrigger: 'hover', value: 'id', label: 'name', children: 'children' }" @change="change"></el-cascader>
</template>

<script>
export default {
  props: {
    value: Number,
    unit: Number,
    type: {
      type: String,
      required: true
    }
  },
  model: {
    prop: 'value',
    event: 'change',
  },
  data: function () {
    return {
      option: [],
      loading: false,
    };
  },
  watch: {
    unit() {
      this.getData()
    },
    type() {
      this.getData()
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    getData() {
      this.loading = true
      let param = { model_type: this.type }
      if (this.unit) {
        param.unit = this.unit
      }
      this.$http.post('/abnormal/tree', param).then(res => {
        if (res.data.code === 0) {
          this.option = res.data.data
        } else {
          this.$message.error(res.data.msg);
        }
        this.loading = false
      }).catch(e => {
        this.loading = false
        this.$message.error(e.message);
      });
    },
    change(val) {
      console.log(val)
      this.$emit('change', val[val.length - 1]);
    }
  },
};
</script>
